#include "ticketinterface.h"

#include <QtCore/QSharedData>
#include <QtCore/QUrl>

class TicketErrorPrivate : public QSharedData
{
public:
    inline TicketErrorPrivate()
    {
        ticket = 0;
    }

    inline TicketErrorPrivate(const TicketErrorPrivate &other)
        : QSharedData(other), type(other.type), error(other.error),
          originalCode(other.originalCode), ticket(other.ticket)
    {

    }

    TicketError::ErrorType type;
    QString error;
    int originalCode;
    const Ticket *ticket;
};

TicketError::TicketError(TicketError::ErrorType type, QString error, int originalCode,
                         const Ticket *ticket)
{
    d = new TicketErrorPrivate;
    d->type = type;
    d->error = error;
    d->originalCode = originalCode;
    d->ticket = ticket;
}

TicketError::TicketError(const TicketError &other)
    : d(other.d)
{
}

TicketError::~TicketError()
{
}

TicketError::ErrorType TicketError::type() const
{
    return d->type;
}

QString TicketError::error() const
{
    return d->error;
}

int TicketError::originalCode() const
{
    return d->originalCode;
}

const Ticket * TicketError::ticket() const
{
    return d->ticket;
}

QDebug operator<<(QDebug d, const TicketError &p)
{
    d.nospace() << "[" << p.type() << "] " << p.error()
                << " (original error code: " << p.originalCode() << ")";
    return d;
}

ITicketExplorerBackend::ITicketExplorerBackend(ITicketBackendConfiguration *config, QObject *parent)
    : QObject(parent), m_config(config)
{
}

 ITicketExplorerBackend::~ITicketExplorerBackend()
 {
    delete m_config;
 }


 QUrl ITicketExplorerBackend::url() const
 {
     return QUrl();
 }

 QString ITicketExplorerBackend::username() const
 {
     return "";
 }

TicketExplorerBackendContext::TicketExplorerBackendContext(ITicketExplorerBackend *backend,
                                                             QObject *parent)
    : QObject(parent), m_backend(backend)
{
}

void TicketExplorerBackendContext::updateHistory(Ticket *ticket) const
{
    if (!m_backend.isNull())
        m_backend.data()->loadHistory(ticket);
}

void TicketExplorerBackendContext::updateActions(Ticket *ticket) const
{
    if (!m_backend.isNull())
        m_backend.data()->loadAvailableActions(ticket);
}

void TicketExplorerBackendContext::storeTicket(Ticket *ticket, const QString &comment) const
{
    if (!m_backend.isNull())
        m_backend.data()->storeTicket(ticket, comment);
}

void TicketExplorerBackendContext::refreshTicket(Ticket *ticket) const
{
    if (!m_backend.isNull())
        m_backend.data()->refreshTicket(ticket);
}

ITicketBackendConfigurationWidgetFactory::ITicketBackendConfigurationWidgetFactory()
{
}






